<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017-03-07
 * Time: 14:09
 */

namespace app\index\controller;



use app\index\Model\Employee;
use think\Controller;
use think\Request;
use think\Db;


class LoginController extends Controller
{
    public function index(){
        return $this->fetch();
    }

    public function login(){
        $data['username'] = Request::instance()->post('username');
        $User = Employee::get($data);
        if($User!==null && $User->getData('password')===md5(Request::instance()->post('password'))){
            session('user',$User->getData('username'));
            return $this->success('登陆成功',url('/'));
        }else{
            return $this->error('账户或密码错误',url('index'));
        }
    }

    public function logout(){
        session('user',null);
        $this->success("退出成功",Url("index"));
    }
    public function add(){
        $data['username'] = Request::instance()->post('username');
        $data['password'] = md5(Request::instance()->post('password'));
        $User = Db::name('Employee')->insert($data);
        if($User=null){
            return json(['data'=>'errer','msg'=>$this->getError()]);
        }else{
            return json(['data'=>'ok','msg'=>1]);
        }
    }

}